﻿


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Permision.DAL;
using System.Data.SqlClient;
using System.Data;
using Tools;
using System.Configuration;
using System.Web.Security;

namespace Permision.BLL
{
    /// <summary>
    /// Summary description for Per_ActionsRoles
    /// </summary>
    public class Per_ActionsRolesManager : Per_ActionsRolesManagerBase
    {
        public Per_ActionsRolesManager()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public static Per_ActionsRolesManager CreateInstant()
        {
            return new Per_ActionsRolesManager();
        }
        public List<Per_ActionsRolesEntity> SearchActionRoles(string ModuleId, string ActionId, string RoleName, string Permissions)
        {
            List<Per_ActionsRolesEntity> lsresult = new List<Per_ActionsRolesEntity>();
            List<SqlParameter> ls = new List<SqlParameter>();
            if (!RoleName.Equals(string.Empty))
                ls.Add(new SqlParameter("@RoleName ", RoleName));
            if (!ActionId.Equals(string.Empty))
                ls.Add(new SqlParameter("@ActionId ", ActionId));
            if (!ModuleId.Equals(string.Empty))
                ls.Add(new SqlParameter("@ModuleId ", ModuleId));
            if (!Permissions.Equals(string.Empty))
                ls.Add(new SqlParameter("@Permissions ", Permissions));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = Tools.SqlHelper.ExecuteDataTable(SqlHelper.TourConnectString, CommandType.StoredProcedure, "per_SelectPer_ActionsRoles", sqlparam);
            if (dt != null && dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(GetPer_ActionsRolesEntity(dt.Rows[i]));
            return lsresult;
        }
        public DataTable SearchActionRolesRDT(string ModuleId, string ActionId, string RoleName, string Permissions)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@RoleName ", RoleName));
            ls.Add(new SqlParameter("@ActionId ", ActionId));
            ls.Add(new SqlParameter("@ModuleId ", ModuleId));
            ls.Add(new SqlParameter("@Permissions ", Permissions));
            SqlParameter[] sqlparam = ls.ToArray();
            return Tools.SqlHelper.ExecuteDataTable(SqlHelper.TourConnectString, CommandType.StoredProcedure, "per_SelectPer_ActionsRoles", sqlparam);
        }
        
        HashSet<string> GetActionsRoles()
        {
            List<Per_ActionsRolesEntity> ActionsRolesCollection = Per_ActionsRolesManager.CreateInstant().SelectAll();
            HashSet<string> AccessSecurity = new HashSet<string>();
            for (int i = 0; i < ActionsRolesCollection.Count; i++)
            {
                string strRole = string.Empty;
                strRole = ActionsRolesCollection[i].RoleName.ToLower();
                strRole = strRole + "-" + ActionsRolesCollection[i].ModuleId.ToLower();
                //if (!ActionsRolesCollection[i].Permissions.Equals("all"))
                strRole = strRole + "-" + ActionsRolesCollection[i].ActionId.ToLower();
                strRole = strRole + "-" + ActionsRolesCollection[i].Permissions.ToLower();
                AccessSecurity.Add(strRole);
            }
            return AccessSecurity;
        }
        HashSet<string> CreateListActionsRoles(string CurrentUserName, string module, string action, string UserName2)
        {
            string[] roleNameCurrentUser = Roles.GetRolesForUser(CurrentUserName);
            string[] roleNameUserName2 = Roles.GetRolesForUser(UserName2);
            HashSet<string> lsReturn = new HashSet<string>();
            for (int i = 0; i < roleNameCurrentUser.Length; i++)
            {
                if (!CurrentUserName.Equals(UserName2))
                    for (int j = 0; j < roleNameUserName2.Length; j++)
                        lsReturn.Add(roleNameCurrentUser[i].ToLower() + "-" + module.ToLower() + "-" + action.ToLower() + "-" + roleNameUserName2[j].ToLower());
                else
                    lsReturn.Add(roleNameCurrentUser[i].ToLower() + "-" + module.ToLower() + "-" + action.ToLower() + "-owner");
                lsReturn.Add(roleNameCurrentUser[i].ToLower() + "-" + module.ToLower() + "-" + action.ToLower() + "-all");
            }
            return lsReturn;
        }
        HashSet<string> CreateListActionsRoles(string CurrentUserName, string module, string action, string UserName2, string[] RolesName)
        {
            return CreateListActionsRoles(CurrentUserName, module, new string[1] { action }, UserName2, RolesName);
        }
        HashSet<string> CreateListActionsRoles(string CurrentUserName, string module, string[] action, string UserName2, string[] RolesName)
        {
            string[] roleNameCurrentUser = Roles.GetRolesForUser(CurrentUserName);
            string[] roleNameUserName2 = RolesName;

            HashSet<string> lsReturn = new HashSet<string>();
            for (int i = 0; i < roleNameCurrentUser.Length; i++)
            {
                if (!CurrentUserName.Equals(UserName2))
                    for (int j = 0; j < roleNameUserName2.Length; j++)
                        for (int k = 0; k < action.Length; k++)
                            lsReturn.Add(roleNameCurrentUser[i].ToLower() + "-" + module.ToLower() + "-" + action[k].ToLower() + "-" + roleNameUserName2[j].ToLower());
                else
                    for (int k = 0; k < action.Length; k++)
                        lsReturn.Add(roleNameCurrentUser[i].ToLower() + "-" + module.ToLower() + "-" + action[k].ToLower() + "-owner");
                for (int k = 0; k < action.Length; k++)
                    lsReturn.Add(roleNameCurrentUser[i].ToLower() + "-" + module.ToLower() + "-" + action[k].ToLower() + "-all");
            }
            return lsReturn;
        }
        /// <summary>
        /// Kiểm tra quyên của user trên module truyền vào
        /// </summary>
        /// <param name="CurrentUserName">User hiện tại</param>
        /// <param name="module">Module</param>
        /// <param name="action">Hành động; vd:Edit, Delete,...</param>
        /// <param name="permissions">bao gồm:owner,all và rolename</param>
        /// <param name="UserName2">owner trên đối tượng đang xét</param>
        /// <returns></returns>
        public bool CheckRolesActions(string CurrentUserName, string module, string action, string UserName2)
        {
            bool result = false;
            string CacheKey = "CheckRolesActions_" + CurrentUserName + "_" + module + "_" + action + "_" + UserName2;
            object cachedRole = CachingHelper.GetCacheSql(CacheKey);
            if (cachedRole != null)
            {
                result = (bool)cachedRole;
            }
            else
            {

                HashSet<string> hsAccessSecurity = GetActionsRoles();
                HashSet<string> lsAccessSecurity = Per_ActionsRolesManager.CreateInstant().CreateListActionsRoles(CurrentUserName, module, action, UserName2);
                foreach (string item in lsAccessSecurity)
                    if (hsAccessSecurity.Contains(item))
                    {
                        result = true; break;
                    }
                int ExpireDuration = int.Parse(ConfigurationManager.AppSettings["ActionRoleExpireDuration"].ToString());
                Tools.CachingHelper.SetNoCacheDependencyObject(CacheKey, result, ExpireDuration);
            }
            return result;
        }
        /// <summary>
        /// Kiểm tra quyên của user trên module truyền vào
        /// </summary>
        /// <param name="CurrentUserName">User hiện tại</param>
        /// <param name="module">Module</param>
        /// <param name="action">Hành động; vd:Edit, Delete,...</param>
        /// <param name="permissions">bao gồm:owner,all và rolename</param>
        /// <param name="UserName2">owner trên đối tượng đang xét</param>
        /// <returns></returns>
        public bool CheckRolesActions(string CurrentUserName, string module, string action, string UserName2, string[] RolesName)
        {
            bool result = false;
            string strRolesName = string.Empty;
            for (int i = 0; i < RolesName.Length; i++)
            {
                strRolesName = strRolesName + "_" + RolesName[i];
            }
            string CacheKey = "CheckRolesActions_" + CurrentUserName + "_" + module + "_" + action + "_" + UserName2 + strRolesName;
            object cachedRole = CachingHelper.GetCacheSql(CacheKey);
            if (cachedRole != null)
            {
                result = (bool)cachedRole;
            }
            else
            {
                HashSet<string> hsAccessSecurity = GetActionsRoles();
                HashSet<string> lsAccessSecurity = Per_ActionsRolesManager.CreateInstant().CreateListActionsRoles(CurrentUserName, module, action, UserName2, RolesName);
                foreach (string item in lsAccessSecurity)
                    if (hsAccessSecurity.Contains(item))
                    {
                        result = true; break;
                    }
                //CachingHelper.CreateCacheSql(CacheKey, result, "Hotel",);
                //ActionRoleExpireDuration
                int ExpireDuration = int.Parse(ConfigurationManager.AppSettings["ActionRoleExpireDuration"].ToString());
                Tools.CachingHelper.SetNoCacheDependencyObject(CacheKey, result, ExpireDuration);
                //HttpContext.Current.Cache.Insert(CacheKey, result);
            }
            return result;
        }
    }
}
